/**
 * cone-service: stringUtil.js
 * Description: 字符串工具类
 * 
 * 
 * Time: 2021/6/19 19:55
 */

const StringUtil = {

    /**
     * 全部替换
     * @param input
     * @param replace
     * @param replaceWith
     * @returns {*}
     */
    replace: function(input, replace, replaceWith){
        if(input){
            return input.replace(new RegExp(replace,"gm"), replaceWith);
        }
        return input;
    },

    /**
     * 删除字符串两边的空白字符
     * @param str
     * @returns {string}
     */
    trim: function (str) {
        if (str) {
            var newStr = str.replace(/(^\s*)|(\s*$)/g, "");
            return newStr;
        }
        return "";
    },

    /**
     * 删除左侧空白字符
     * @param str
     * @returns {string}
     */
    ltrim: function (str) {
        if (str) {
            var newStr = str.replace(/(^\s*)/g, "");
            return newStr;
        }
        return "";
    },

    /**
     * 删除右边的空白字符
     * @param str
     * @returns {string}
     */
    rtrim: function (str) {
        if (str) {
            var newStr = str.replace(/(\s*$)/g, "");
            return newStr;
        }
        return "";
    },

    /**
     * 时间格式化
     * @param timestampOrdate
     * @param format 'YYYY-MM-DD hh:mm:ss'
     * @returns {*}
     */
    formatDate:function(timestampOrdate, format='YYYY-MM-DD hh:mm:ss'){
        let _day = null;
        if(timestampOrdate != undefined){
            if(timestampOrdate instanceof Date){
                _day = timestampOrdate;
            }
            else{
                _day = new Date(timestampOrdate);
            }
        }
        else{
            _day = new Date();
        }

        let date = {
            "M+": _day.getMonth() + 1,
            "D+": _day.getDate(),
            "h+": _day.getHours(),
            "m+": _day.getMinutes(),
            "s+": _day.getSeconds(),
            "S+": _day.getMilliseconds()
        };
        if (/(Y+)/i.test(format)) {
            format = format.replace(RegExp.$1, (_day.getFullYear() + '').substr(4 - RegExp.$1.length));
        }
        for (let k in date) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1
                    ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
            }
        }
        return format;
    },

    /**
     * 判断是否为数字
     * @param object
     * @returns {boolean}
     */
    isNumber:function(object){
        return (typeof object === 'number' && !isNaN(object));
    },

    /**
     * 判断是否为数组
     * @param object
     * @returns {*|boolean}
     */
    isArray:function(object){
        if(Array.isArray){
            return Array.isArray(object);
        }
        else{
            return object && typeof object === 'object' && Array == object.constructor;
        }
    },

    /**
     * 判断是否为字符串
     * @param object
     * @returns {boolean}
     */
    isString:function(object){
        return object && typeof object === "string" && String == object.constructor;
    },

    /**
     * 判断是否为Objec对象
     * @param object
     * @returns {*|boolean}
     */
    isObject:function(object){
        return object && typeof object === "object" && Object == object.constructor;
    },

    /**
     * 判断是否为Function对象
     * @param object
     * @returns {*|boolean}
     */
    isFunction:function(object){
        return object && typeof object === "function" && Function == object.constructor;
    },

    /**
     * jquery的extend的大致实现
     * @returns {Object|{}|any}
     */
    extend:function(){
        var options, name, src, copy, copyIsArray, clone,
            target = arguments[0] || {},
            i = 1,
            length = arguments.length,
            deep = false;

        if ( typeof target === "boolean" ) {
            deep = target;
            target = arguments[1] || {};
            // skip the boolean and the target
            i = 2;
        }

        if ( typeof target !== "object" && !StringUtil.isFunction(target) ) {
            target = {};
        }

        for ( ; i < length; i++ ) {
            // Only deal with non-null/undefined values
            if ( (options = arguments[ i ]) != null ) {
                // Extend the base object
                for ( name in options ) {
                    src = target[ name ];
                    copy = options[ name ];

                    // Prevent never-ending loop
                    if ( target === copy ) {
                        continue;
                    }

                    // Recurse if we're merging plain objects or arrays
                    if ( deep && copy && ( StringUtil.isObject(copy) || (copyIsArray = StringUtil.isArray(copy)) ) ) {
                        if ( copyIsArray ) {
                            copyIsArray = false;
                            clone = src && StringUtil.isArray(src) ? src : [];

                        } else {
                            clone = src && StringUtil.isObject(src) ? src : {};
                        }

                        // Never move original objects, clone them
                        target[ name ] = StringUtil.extend( deep, clone, copy );

                        // Don't bring in undefined values
                    } else if ( copy !== undefined ) {
                        target[ name ] = copy;
                    }
                }
            }
        }

        // Return the modified object
        return target;
    },

    /**
     * 返回min~max之间的随机数
     * @param {*} min 
     * @param {*} max 
     * @returns 
     */
    random: function(min, max) {
        return Math.round(Math.random() * (max - min)) + min;
    }
}

module.exports = StringUtil;